package com.itheima.test.newcustomer;

public class Foodie extends Thread{
    private Desk desk;
    public Foodie(Desk desk) {
        this.desk = desk;
    }
    @Override
    public void run() {
        while(true){
            synchronized(desk.getLock()){
                if(desk.getHamburgerNumber() == 0){//汉堡包数量为0时退出
                    break;
                }else{
                    if(desk.isFlag()){//桌子上有汉堡则吃汉堡
                        System.out.println("宅男吃货正在吃汉堡");
                        desk.setHamburgerNumber(desk.getHamburgerNumber()-1);//汉堡数量减一
                        desk.setFlag(false);//桌子上汉堡状态为无
                        desk.getLock().notifyAll();//叫醒厨师
                    }else{//桌子上无汉堡则等待
                        try {
                            desk.getLock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
